/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.duricomeilmetallo.jpdp8;


/**
 *
 * @author DCM
 */
public class BitArray {

    Boolean array[];

    public BitArray(int length) {
        this.array = new Boolean[length];
        for (int i = 0; i < length; i++) {
            array[i] = false;
        }
    }

    public BitArray(int length, int value) {
        this.array = new Boolean[length];
        assign(value);
    }

    public void increment() throws IncompatibleLengthException {
        add(new BitArray(array.length, 1));
    }

    public boolean add(BitArray b) throws IncompatibleLengthException {
        if (array.length != b.array.length) {
            throw new IncompatibleLengthException(array.length, b.array.length);
        }
        boolean riporto = false;
        boolean temp;
        for (int i = b.array.length - 1; i >= 0; i--) {
            temp = array[i] ^ b.array[i] ^ riporto;
            //if (array[i]&&b.array[i]||b.array[i]&&riporto||array[i]&&riporto) {riporto=true;}
            //else riporto=false;
            riporto = (array[i] && b.array[i]) || (riporto && (array[i] ^ b.array[i]));
            array[i] = temp;
        }
        return riporto;
    }

    public void assign(int x) {
        String temp = Integer.toBinaryString(x);
        for (int i = 0; i < array.length; i++) {
            array[i] = false;
        }
        //debug System.out.println(temp);
        int j = temp.length() - 1;
        int i = array.length - 1;
        while (j >= 0 && i >= 0) {
            //debug  System.out.println(Integer.parseInt(temp.charAt(j)+""));
            if (Integer.parseInt(temp.charAt(j) + "") == 0) {
                array[i] = false;
            } else {
                array[i] = true;
            }
            j--;
            i--;
        }
    }

    public void assign(String hexValue) {
        assign(Integer.parseInt(hexValue, 16));
    }
    public void assign(Boolean[] booleanArray) throws IncompatibleLengthException {
            if (array.length != booleanArray.length) {
            throw new IncompatibleLengthException(array.length, booleanArray.length);
        }
            else {
                for (int i=0;i<array.length;i++) {
                    array[i]=booleanArray[i];
                }
            }
    }
    public void assign(BitArray b) throws IncompatibleLengthException {
        if (array.length != b.array.length) {
            throw new IncompatibleLengthException(array.length, b.array.length);
        }
        else {
        for (int i = 0; i < array.length; i++) {
            array[i] = b.array[i];
        }
        }
    }

    public String getBinaryString() {
        String risultato = "";
        for (int i = 0; i < array.length; i++) {
            if (array[i] == true) {
                risultato = risultato + "1";
            } else {
                risultato = risultato + "0";
            }
        }
        return risultato;
    }
    void getIntegerValue() {
    for (int i=0; i==array.length; i++){
    if (array[i]==true)
        array[i]=false;
    else array[i]=true;
    
    }
    }
}
